/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.U.
 */
package es.silenus.detecta.batch.mapper;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.batch.item.file.transform.IncorrectTokenCountException;


/**
 * Non-strict delimited line tokenizer.
 *
 * @author <a href="mailto:malonso@silenus-consultoria.es">Mariano Alonso</a>
 *
 * @since 24-ago-2009 19:01:38
 */
public class NonStrictDelimitedLineTokenizer extends DelimitedLineTokenizer {
	private static final Log LOG = LogFactory.getLog(NonStrictDelimitedLineTokenizer.class);

/**
   * Constructor.
   */
	public NonStrictDelimitedLineTokenizer() {
	}

/**
   * Constructor.
   *
   * @param delimiter delimiter.
   */
	public NonStrictDelimitedLineTokenizer(char delimiter) {
		super(delimiter);
	}

	/**
	 * Yields the tokens resulting from the splitting of the supplied <code>line</code>.
	 *
	 * @param line the line to be tokenised (can be <code>null</code>)
	 *
	 * @return the resulting tokens
	 */
	public FieldSet tokenize(String line) {
		try {
			return super.tokenize(line);
		} catch(IncorrectTokenCountException e) {
			if(LOG.isDebugEnabled()) {
				LOG.debug(String.format("Discarding line %s", line), e);
			}

			return null;
		}
	}
}
